package com.example.reggie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.example.reggie.entity.ShoppingCart;
import com.example.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;
    /**
     * 添加购物车
     * 思路：
     *      1.设置用户id，userId
     *      2.判断添加的是菜品还是套餐
     *             通过获取dishId 判断是否为空
     *      3.查询添加的菜品或套餐是否在购物车中
     *          查询字段：
     *              userId + DishId/SetmealId
     *          Y: 数量+1
     *          N：添加购物车 数量=1 number =1  createTime = now
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        //设置用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //  设置ShoppingCart表的条件构造器
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null)
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        else
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (shoppingCartServiceOne != null){
            Integer number = shoppingCartServiceOne.getNumber();
            shoppingCartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCartServiceOne);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartServiceOne = shoppingCart;
        }
        return R.success(shoppingCartServiceOne);
    }

    /**
     * 购物车展示
     * 根据用户id  userId 查询，根据创建时间 createTime 排序
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     * 根据用户Id ，userId查询
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清空成功");
    }

    /**
     * 减少数量
     * 思路：
     *      提供dishId  /  setmealId
     *      1.判断是菜品还是套餐
     *          获取id ，判断id是否为空
     *      2.获取ShoppingCart表中对应的数据，获取number ，number-1
     *      3.更新修改过number的数据
     *      4.将所有number =1 的数据清除
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("");
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);

        if(dishId != null)
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        else
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,setmealId);

        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);

        Integer number = shoppingCartServiceOne.getNumber();
        shoppingCartServiceOne.setNumber(number -1);
        shoppingCartService.updateById(shoppingCartServiceOne);
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(ShoppingCart::getNumber,0);
        shoppingCartService.remove(lambdaQueryWrapper1);
        return R.success("减少成功");
    }
}
